package com.zackeus.sipservice.entity;

import android.os.Parcel;
import android.os.Parcelable;

import androidx.annotation.NonNull;

import com.zackeus.common.utils.StringUtils;
import com.zackeus.sipservice.SipUri;
import com.zackeus.sipservice.constant.SipStateCode;
import com.zackeus.sipservice.constant.SipStatusCode;

import org.apache.commons.lang3.builder.ReflectionToStringBuilder;

/**
 * SIP 会话信息
 * @name: SipCallInfo
 * @author: zz
 * @date: 2022/11/1
 */
public class SipCallInfo implements Parcelable {

    private final String accountId;
    private final Integer callId;
    private final Integer callRole;
    private final @SipStateCode.Unit
    int callState;
    private final @SipStatusCode.Unit
    int callStatus;

    private final String userName;
    private final String displayName;
    /* 接通时间戳 */
    private final long confirmedTimestamp;
    /* 当前会话是否活跃 */
    private final boolean active;

    public SipCallInfo(String accountId) {
        this(accountId, null, null, SipStateCode.SIP_NULL, SipStatusCode.SIP_NULL,
                "", 0, Boolean.FALSE);
    }

    public SipCallInfo(String accountId, Integer callId, Integer callRole,
                       @SipStateCode.Unit int callState,  @SipStatusCode.Unit int callStatus,
                       String contact, long confirmedTimestamp, boolean active) {
        this.accountId = accountId;
        this.callId = callId;
        this.callRole = callRole;
        this.callState = callState;
        this.callStatus = callStatus;

        if (StringUtils.isEmpty(contact) || StringUtils.isBlank(contact)) {
            this.userName = "Unknown";
            this.displayName = "Unknown";
        } else {
            SipUri.SipContactInfo contactInfo = SipUri.parseSipContact(contact);
            this.userName = contactInfo.userName;
            this.displayName = contactInfo.displayName;
        }
        this.confirmedTimestamp = confirmedTimestamp;
        this.active = active;
    }

    protected SipCallInfo(Parcel in) {
        accountId = in.readString();
        callId = (Integer) in.readValue(Integer.class.getClassLoader());
        callRole = (Integer) in.readValue(Integer.class.getClassLoader());
        callState = in.readInt();
        callStatus = in.readInt();
        userName = in.readString();
        displayName = in.readString();
        confirmedTimestamp = in.readLong();
        active = in.readByte() != 0;
    }

    public static final Creator<SipCallInfo> CREATOR = new Creator<>() {
        @Override
        public SipCallInfo createFromParcel(Parcel in) {
            return new SipCallInfo(in);
        }

        @Override
        public SipCallInfo[] newArray(int size) {
            return new SipCallInfo[size];
        }
    };

    public String getAccountId() {
        return accountId;
    }

    public Integer getCallId() {
        return callId;
    }

    public Integer getCallRole() {
        return callRole;
    }

    @SipStateCode.Unit
    public int getCallState() {
        return callState;
    }

    @SipStatusCode.Unit
    public int getCallStatus() {
        return callStatus;
    }

    public String getUserName() {
        return userName;
    }

    public String getDisplayName() {
        return displayName;
    }

    public long getConfirmedTimestamp() {
        return confirmedTimestamp;
    }

    public boolean isActive() {
        return active;
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel parcel, int i) {
        parcel.writeString(accountId);
        parcel.writeValue(callId);
        parcel.writeValue(callRole);
        parcel.writeInt(callState);
        parcel.writeInt(callStatus);
        parcel.writeString(userName);
        parcel.writeString(displayName);
        parcel.writeLong(confirmedTimestamp);
        parcel.writeByte((byte) (this.active ? 1 : 0));
    }

    @NonNull
    @Override
    public String toString() {
        return ReflectionToStringBuilder.toString(this);
    }
}
